<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp"%>
<%@ include file="/commons/tag.jsp"%>
<%@ include file="/commons/js/basejs.jsp"%>
<%@ include file="/commons/js/extjs.jsp"%>
<%@ include file="/commons/css/basecss.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<script type="text/javascript">
		$(function(){
			debugger;
			init_datagrid('${path}');
			$("#addDiv").css("display", "none"); //隐藏添加对话框
			$("#modifyDiv").css("display", "none"); //隐藏修改对话框
		});
		
		//手机号码的正则验证
		$.extend($.fn.validatebox.defaults.rules, {    
		    phoneNum: { //验证手机号   
		        validator: function(value, param){ 
		         return /^1[3-8]+\d{9}$/.test(value);
		        },    
		        message: '请输入正确的手机号码。'   
		    }
		});
		
		//显示弹出添加的对话框  
		function showAddFrm() {  
		     $("#addDiv").css("display", "block");
		     $("#addDiv").dialog({  
		         width: 400,  
		         height: 300,
		         modal: true,  
		         title: "添加厂商信息",  
		         collapsible: true,  
		         minimizable: true,  
		         maximizable: true,  
		         resizable: true,  
		         buttons: [{  
		             id: 'btnAdd',  
		             text: '添加',  
		             iconCls: 'icon-add',  
		             handler: function () {  		             
		                 $("#addform").submit();  //让表单提交  
		             }  
		         }, {  
		             id: 'btnCancelAdd',  
		             text: '取消',  
		             iconCls: 'icon-cancel',  
		             handler: function () {
		            	 $('#addform').form('clear'); //清除表单数据
		                 $("#addDiv").dialog("close");  
		             }  
		         }]  
		     }); 
		}
		
		//显示弹出修改的对话框
		function showModifyFrm(line) {  
		     $("#modifyDiv").css("display", "block");
		     $('#modifyform').form('load',line);
		     $("#modifyDiv").dialog({  
		         width: 400,  
		         height: 300,
		         modal: true,  
		         title: "修改厂商信息",  
		         collapsible: true,  
		         minimizable: true,  
		         maximizable: true,  
		         resizable: true,  
		         buttons: [{  
		             id: 'btnEdit',  
		             text: '修改',  
		             iconCls: 'icon-edit',  
		             handler: function () {  		             
		                 $("#modifyform").submit();  //让表单提交  
		             }   
		         }, {  
		             id: 'btnCancelAdd',  
		             text: '取消',  
		             iconCls: 'icon-cancel',  
		             handler: function () {		     
		                 $("#modifyDiv").dialog("close");  
		             }  
		         }]  
		     }); 
		}
		
		//回显
		function find(){
			var line = $('#table1').datagrid("getSelected");
			if(line == null){
				$.messager.alert("提示消息", "请选择需要编辑的条目！");
			}else{
				showModifyFrm(line);
			}			
		}
		
		//条件查询
		function doSearch(){			
			$('#table1').datagrid('load',{
				keyword: $('#keyword').val(),
				beginTime: $('#beginTime').datebox('getValue'),
				endTime: $('#endTime').datebox('getValue'),
			});
		}

		//启动表格
		function init_datagrid(path) {
			//控制addform表单的提交
			$('#addform').form({    
			    url: path+'/factoryCreate/insert',
			    success:function(data){   
			    	var json = JSON.parse(data);
			    	if(json.success){
				        /* $("#addDiv").dialog("close");  //关闭对话框   		         
				        $('#table1').datagrid("reload");  //刷新表 
				        $('#addform').form('clear'); //清除表单数据 */
			    		window.location.href="${path}/factoryCreate/facInfo";
			        }else{
			        	$.messager.alert("提示消息", "保存失败："+json.message);  
			        }   
			    }    
			});
			
			//控制modifyform表单的提交
			$('#modifyform').form({    
			    url: path+'/factoryCreate/update',
			    success:function(data){
			    	var json = JSON.parse(data);
			        if(json.success){
			        	window.location.href="${path}/factoryCreate/facInfo";
			        }else{
			        	$.messager.alert("提示消息", "保存失败："+json.message);  
			        }   
			    }    
			});
			
			$('#table1').datagrid({
				url: path+'/factoryCreate/selectPage',
				fit: true,
				border: false,
				pagination: true,
				pagePosition: 'bottom',
				idField: 'id',
				pageSize: 10,
				pageList: [10,20,30,40,50],
				checkOnSelect: true,
				selectOnCheck: true,
				singleSelect: false,
				nowrap: true, 
				rownumbers: true, //显示行号
				showfooter: true, //显示行尾
				pageNumber: 1,
				fitColumns: true,
				sortName: 'updateTime',	//按字段排序
				sortOrder: 'DESC',		//倒序
				columns : [[
				 {field : 'id', title :'id', width : 50, checkbox: true},
				 {field : 'facNo', title:'编号', width : '10%', align : 'center', sortable:true,},
				 {field : 'facName', title:'名称', width : '35%', align : 'center',sortable:true,},				
				 {field : 'contact', title:'联系人', width : '10%', align : 'center',sortable:true},
				 {field : 'tel', title:'联系电话', width : '15%', align : 'center',sortable:true},
				 {field : 'email', title:'Email', width : '15%', align : 'center',sortable:true},
				 {field : 'action', title:'操作', width: '15%', align:'center',
						formatter: function(value,row,index){
							var e = '<a href="javascript:void(0)" onclick="find(this)">编辑</a> ';
							return e;
						}
					}]],
				toolbar : '#datagrid_toolbar'
		   });
		};

	 	//删除数据  
	    function doDelete() {  
		     var selectRows = $('#table1').datagrid("getSelections");  //把选中的数据查询出来。    
		     if (selectRows.length < 1) {  
		         $.messager.alert("提示消息", "请选择需要删除的条目！");  
		         return;  
		     }  
	     	 //提醒用户是否是真的删除数据  
	     	 $.messager.confirm("确认消息", "您确定要删除吗？", function (r) {  
		         if (r) {  
		             //真删除了  1,3,4  
		             var strIds = "";  
		             for (var i = 0; i < selectRows.length; i++) {  
		                 strIds += selectRows[i].id + ",";  
		             }  
		             strIds = strIds.substr(0, strIds.length - 1);  
		             $.post("/factoryCreate/delBy", { ids: strIds }, function (data) {
		                 if (data == "ok") {   
		                     $('#table1').datagrid("reload");  //刷新表格
		                     $('#table1').datagrid("clearSelections");  //去掉选中状态的那些行
		                 } else {  
		                     $.messager.alert("提示消息", "删除失败："+data);  
		                 }  
		             });  
		         }  
	     	});
	    }
	</script>

	<div class="easyui-layout" data-options="fit:true,border:false">
		
		<!-- 表头模糊、条件查询 -->
		<div data-options="region:'north',border:false" class="top_sreach" style="height:35px">
			<form id="searchform" method="POST">
				<table>
					<tr>				
						<td>关键字：</td>
						<td>
							<input class="text" id="keyword" name="keyword" class="easyui-textbox" placeholder="包括厂商名称/编码" />
						</td>
						<td>填报日期：</td>
						<td>
							<input type="text" id="beginTime" name="beginTime" class="easyui-datebox" data-options="editable:true"/>
						</td>
						<td>-----至：</td>
						<td>
							<input type="text" id="endTime" name="endTime" class="easyui-datebox" data-options="editable:true"/>
						</td>
						<td>							
							<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="doSearch()">搜索</a>
						</td>
					</tr>
	            </table>
	        </form>
    	</div>
	
		<!-- 主数据表 -->
		<div data-options="region:'center',border:false" style="width: 100%;">
			<table id="table1" data-options="fit:true,border:false"></table>
		</div>

		<!-- 菜单栏功能按钮 -->
		<div id="datagrid_toolbar">
			<a onclick="showAddFrm();" href="javascript:void(0);" class="easyui-linkbutton" 
				data-options="plain:true,iconCls:'icon-add'">添加</a>
			<a onclick="find(this)" href="javascript:void(0);" class="easyui-linkbutton" 
				data-options="plain:true,iconCls:'icon-edit'">编辑</a>
	        <a onclick=" " href="javascript:void(0);" class="easyui-linkbutton" 
	        	data-options="plain:true,iconCls:'icon-search'">查看</a>
	       	<a onclick="doDelete();" href="javascript:void(0);" class="easyui-linkbutton" 
	       		data-options="plain:true,iconCls:'icon-del'">删除</a>
		</div>
		
		<!-- 新增表单 -->
		<div id="addDiv">  
            <form id="addform" method="post">
            	<table>
            		<tr>
            			<td><label for="facNo">编号</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="facNo" data-options="required:true" /></td>
            		</tr>
            		<tr>
            			<td><label for="facName">名称</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="facName" data-options="required:true" /></td>
            		</tr>
            		<tr>
            			<td><label for="contact">联系人</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="contact" data-options="required:true" /></td>
            		</tr>
            		<tr>
            			<td><label for="tel">联系电话</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="tel" 
            					 data-options="required:true, validType:'phoneNum'" /></td>
            		</tr>
            		<tr>
            			<td><label for="email">Email</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="email" data-options="validType:'email'" /></td>
            		</tr>
            	</table>
			</form> 
    	</div>
    	
    	<!-- 修改表单 -->
    	<div id="modifyDiv">  
            <form id="modifyform" method="post">
            	<input class="easyui-validatebox" type="text" name="id" data-options="required:true" hidden="true"/>
            	<table>
            		<tr>
            			<td><label for="facNo">编号</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="facNo" data-options="required:true" /></td>
            		</tr>
            		<tr>
            			<td><label for="facName">名称</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="facName" data-options="required:true" /></td>
            		</tr>
            		<tr>
            			<td><label for="contact">联系人</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="contact" data-options="required:true" /></td>
            		</tr>
            		<tr>
            			<td><label for="tel">联系电话</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="tel" 
            					 data-options="required:true, validType:'phoneNum'" /></td>
            		</tr>
            		<tr>
            			<td><label for="email">Email</label></td>
            			<td>：</td>
            			<td><input class="easyui-textbox" type="text" name="email" data-options="validType:'email'" /></td>
            		</tr>
            	</table>           	
			</form> 
    	</div>
    	
	</div>
</html>